import { login, tokenLogin } from "@/api/user";
import router from "@/router";
import Layout from "@/layout";
import { getToken, setToken } from "@/utils/auth";
import { ansytRoutes } from "@/router/index";
import { Message } from "element-ui";
const state = {
  token: localStorage.getItem("HSKJ-token") || "",
  // token: getToken() || "",
  router: [] || localStorage.getItem("HSKJ-router-list"), //用户权限路由地址
  userName: JSON.parse(localStorage.getItem("HSKJ-username")) || "",
};
const mutations = {
  setToen(state, newToken) {
    state.token = newToken.token;
    state.router = newToken.router;
    state.userName = newToken.username;
    localStorage.setItem("HSKJ-token", newToken.token);
    localStorage.setItem("HSKJ-username", JSON.stringify(newToken.username));
    localStorage.setItem("HSKJ-router-list", JSON.stringify(newToken.router));

    //动态路由
    console.log(16, newToken.router);
  },
};
const actions = {
  //登录
  vueXLogin(store, loginForm) {
    login(loginForm).then((res) => {
      // 存储toekn
      store.commit("setToen", res.data.data);
      //获取路由
      if (res.data.meta.status == 200) {
        let ajaxRouterData = res.data.data.router;
        let ajaxRouterList = [];

        console.log(36, res);
        //后台没有路由
        if (res.data.data.router[0] === null) {
          //动态路由匹配404页面
          let error = { path: "*", redirect: "/404", hidden: true };

          ajaxRouterList.push(error);

          let routerList = router.options.routes.concat(ajaxRouterList);

          router.options.routes = routerList;

          router.addRoutes(ajaxRouterList);

          // 跳转首页
          router.push("/");

          Message.success('登录成功')

          return;
        }

        //有路由
        ajaxRouterData.forEach((item) => {
          ajaxRouterList.push({
            path: `/${item.routing_path}`,
            component: Layout,
            meta: { title: item.name, icon: item.icon },
            redirect: `/${item.routing_path}`,
            children: [
              {
                path: `${item.routing_path}`,
                name: "Setting",
                // component: store.commit("loadView", item.routing_path),
                // component: () => import("@/views/setting/index"),
                component: (resolve) =>
                  require([`@/views/${item.routing_path}/index.vue`], resolve),
                meta: { title: item.name, icon: item.icon },
              },
            ],
          });
        });

        //动态路由匹配404页面
        let error = { path: "*", redirect: "/404", hidden: true };

        ajaxRouterList.push(error);

        let routerList = router.options.routes.concat(ajaxRouterList);

        console.log(55, routerList);

        router.options.routes = routerList;

        router.addRoutes(ajaxRouterList);

        //  模拟动态路由
        // let ansytRoutes = [{
        //   path: "/testsd",
        //   component: Layout,
        //   meta: { title: "首页1111", icon: "dashboard" },
        //   redirect: "/testsd/",
        //   children: [
        //     {
        //       path: "testsd",
        //       name: "Testsd",
        //       component: () => import("@/views/testsd/index"),
        //       meta: { title: "首页1111", icon: "dashboard" },
        //     },
        //   ],
        // },{ path: "*", redirect: "/404", hidden: true }]

        // let data =  router.options.routes.concat(ansytRoutes)

        // router.options.routes = data

        // router.addRoutes(ansytRoutes)

        // 跳转首页
        router.push("/");

        Message.success('登录成功')
      }
    });
  },
  //token登录
  async vueXtokenLogin(store) {
    let res = JSON.parse(localStorage.getItem("HSKJ-router-list"));

    console.log(121, res);

    if (res[0] === null) {
      let ajaxRouterList = [];
      //动态路由匹配404页面
      let error = { path: "*", redirect: "/404", hidden: true };

      ajaxRouterList.push(error);

      let routerList = router.options.routes.concat(ajaxRouterList);

      router.options.routes = routerList;

      const uniqueArr = Array.from(
        new Set(router.options.routes.map((obj) => JSON.stringify(obj)))
      ).map((str) => JSON.parse(str));

      router.addRoutes(routerList);

      return;
    }

    let ajaxRouterData = res;
    state.router = ajaxRouterData;
    let ajaxRouterList = [];
    ajaxRouterData.forEach((item) => {
      ajaxRouterList.push({
        path: `/${item.routing_path}`,
        component: Layout,
        meta: { title: item.name, icon: item.icon },
        redirect: `/${item.routing_path}`,
        children: [
          {
            path: `${item.routing_path}`,
            name: item.routing_path,
            component: (resolve) =>
              require([`@/views/${item.routing_path}/index.vue`], resolve),
            meta: { title: item.name, icon: item.icon },
          },
        ],
      });
    });

    //动态路由匹配404页面
    let error = { path: "*", redirect: "/404", hidden: true };

    ajaxRouterList.push(error);

    let routerList = router.options.routes.concat(ajaxRouterList);

    router.options.routes = routerList;

    const uniqueArr = Array.from(
      new Set(router.options.routes.map((obj) => JSON.stringify(obj)))
    ).map((str) => JSON.parse(str));

    router.addRoutes(routerList);
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
